package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Component
@Slf4j
public class Template9 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    //文字替换
    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("8KEY-1")){
                        textRun.setText("本周新增用户画像总结：");
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-2")){
                        String wtdText1 = data.get("wtdText1").asText();
                        templateUtil.text(textParagraph,wtdText1,false);
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-10")){
                        String wtdText2 = data.get("wtdText2").asText();
                        templateUtil.text(textParagraph,wtdText2,false);
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-4")){
                        textRun.setText("YTD用户画像总结：");
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-5")){
                        String ytdText1 = data.get("ytdText1").asText();
                        templateUtil.text(textParagraph,ytdText1,false);
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-12")){
                        String ytdText2 = data.get("ytdText2").asText();
                        templateUtil.text(textParagraph,ytdText2,false);
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-7")){
                        textRun.setText("YTD会员用户等级：");
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-8")){
                        String vipText = data.get("vipText").asText();
                        textRun.setText(vipText);
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-9")){
                        textRun.setText(data.get("ytdDataPeriod").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-19")){
                        textRun.setText(data.get("wtdDataPeriod").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("8KEY-15")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("页码:{},异常信息:{}",9,e);
                }
            }
        }
    }

    //柱图
    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        String dataName;
        String jsonKey;
        switch (plot.getBarChartList().get(0).getSerList().get(0).getVal().getNumRef().getNumCache().getPtArray(0).getV()) {
            case "63389":
                dataName = "年龄分布";
                jsonKey = "ytdAge";
                break;
            case "16261":
                dataName = "年龄分布";
                jsonKey = "wtdAge";
                break;
            case "36242":
                dataName = "地域分布";
                jsonKey = "ytdArea";
                break;
            case "8237":
                dataName = "地域分布";
                jsonKey = "wtdArea";
                break;
            default:
                dataName = "用户数";
                jsonKey = "vip";
                break;
        }
        List<SeriesData> pieData = getData(data,jsonKey,dataName,0);
        templateUtil.barChart(plot,pieData,workbook,sheet,chart);
    }

    private List<SeriesData> getData(JsonNode data, String jsonKey, String dataName, int i) {
        JsonNode jsonNode = data.get(jsonKey);
        List<NameDouble> nameDoubles = new ArrayList<>();
        List<SeriesData> seriesDatas = new ArrayList<>();
        for (JsonNode node : jsonNode) {
            NameDouble nameDouble = new NameDouble(node.get("tempName").asText(),node.get("tempCount").asDouble());
            nameDoubles.add(nameDouble);
        }
        SeriesData seriesData = new SeriesData(dataName,nameDoubles,"");
        seriesDatas.add(seriesData);
        return seriesDatas;
    }

    public void pieChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        String dataName = "年龄分布";
        String jsonKey;
        List<SeriesData> pieData;
        if (plot.getPieChartList().get(0).getSerList().get(0).getVal().getNumRef().getNumCache().getPtArray(0).getV().equals("941")) {
            jsonKey = "ytdGender";
            pieData = getData(data,jsonKey,dataName,1);
        } else {
            jsonKey = "wtdGender";
            pieData = getData(data,jsonKey,dataName,1);
        }

        templateUtil.pieChart(plot, pieData,workbook,sheet,chart);
    }
}
